package com.jimmyhsu.ecnudaowei.Utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.ImageView;

import com.jimmyhsu.ecnudaowei.common.views.RoundDrawable;
import com.jimmyhsu.ecnudaowei.login.LoginActivity;

import java.io.InputStream;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Streaming;
import retrofit2.http.Url;

/**
 * Created by jimmyhsu on 2017/3/3.
 */

public class DownloadUtils {
    public static void download(String url, String path) {

    }

    public static void downloadImageRound(final String url, final ImageView iv) {
        ImageDownloadService imageDownloadService = new Retrofit.Builder()
                .baseUrl(LoginActivity.BASE_URL)
                .build()
                .create(ImageDownloadService.class);
        Call<ResponseBody> call = imageDownloadService.downloadImage(url);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.isSuccessful()) {
                    InputStream is = response.body().byteStream();
                    Bitmap bm = BitmapFactory.decodeStream(is);
                    if (bm != null) {
                        RoundDrawable drawable = new RoundDrawable(bm);
                        iv.setImageDrawable(drawable);
                    }
                } else {
                    Log.e("DownloadUtils", response.code()+" failed: " + url);
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    public static RequestBody getFormRequestBody(String s) {
        return RequestBody.create(
                        MediaType.parse("multipart/form-data"), s);
    }

    interface ImageDownloadService {

        @GET
        Call<ResponseBody> downloadImage(@Url String path);
    }
}
